-module(xmerl_tools).

-include_lib("xmerl/include/xmerl.hrl").
-export([remove_empty/1]).

remove_empty(E) when record(E,xmlElement) ->
    NewContent = remove_empty(E#xmlElement.content),
    E#xmlElement{content=NewContent};
remove_empty(List) when list(List) ->
    Filter =
	fun(X) when record(X,xmlText) ->
		not whitespace_only(X#xmlText.value);
	   (X) ->
		true
	end,
    NewContent = .lists:filter(Filter,append_text(List)),
    Map =
	fun(X) ->
		remove_empty(X)
	end,
    .lists:map(Map,NewContent);
remove_empty(Other) ->
    Other.

whitespace_only([]) ->
    true;
whitespace_only([H|T]) ->
    case
	is_whitespace(H) of
	true ->
	    whitespace_only(T);
	false ->
	    false
    end.

is_whitespace(C) ->
    .lists:member(C," \t\r\n").

append_text([]) ->
    [];
append_text([H|T]) when record(H,xmlText) ->
    case append_text(T,H#xmlText.value) of
	not_all_text ->
	    [H|T];
	Text ->
	    [H#xmlText{value=Text}]
    end;
append_text(Other) ->
    Other.

append_text([],Res) ->
    Res;
append_text([H|T],Res) when record(H,xmlText) ->
    append_text(T,Res++H#xmlText.value);
append_text(_,_) ->
    not_all_text.

